#ifndef GAME_POOL_TABLE_H
#define GAME_POOL_TABLE_H

#include "../Graphics/Drawable.h"
#include "../Math/Size.h"
#include "../Math/Vector.h"
#include "Ball.h"

using namespace Math;

namespace Game
{
	class PoolTable: public Graphics::Drawable
	{
		public:
				PoolTable(const Size& s=Size(), Graphics::Renderable* parent=NULL);

				//Override Renderable::redraw()
				void redraw();

				Math::Size size();

				void setSize(const Math::Size& s);

				Vector* collide(Game::Ball& b)
				{
					Point c=b.center();

					Point TopLeft=Point(_position.x() - _size.width()/2.0f,0.0f, _position.z() - _size.height()/2.0f);
					Point TopRight=Point(_position.x()+ _size.width()/2.0f,0.0f, _position.z() - _size.height()/2.0f);

					//Point BottomLeft=Point(_position.x() - _size.width()/2.0f,0.0f, _position.z() + _size.height()/2.0f);
					Point BottomRight=Point(_position.x()+ _size.width()/2.0f,0.0f, _position.z() + _size.height()/2.0f);

					Vector* result=NULL;

					//Left
					if (TopLeft.x()>=c.x())
					{
						b.position()->setX(TopLeft.x());
						result=new Vector(b.direction());
						result->setX(result->x()*-1.0f);
						return result;
					}
					//Top
					if (TopLeft.z()>=c.z())
					{
						b.position()->setZ(TopLeft.z());
						result=new Vector(b.direction());
						result->setZ(result->z()*-1.0f);
						return result;
					}
					//Right
					if (TopRight.x()<=c.x())
					{
						b.position()->setX(TopRight.x());
						result=new Vector(b.direction());
						result->setX(result->x()*-1.0f);
						return result;
					}
					//Bottom
					if (BottomRight.z()<=c.z())
					{
						b.position()->setZ(BottomRight.z());
						result=new Vector(b.direction());
						result->setZ(result->z()*-1.0f);
						return result;
					}

					return result;
				}


		private:
				Math::Size _size;

				//From Assignment #2
				float _collide(Point& p1, Point& p2, Game::Ball& b)
				{
					Point v1(b.center().x() - p1.x(), 0.0f, b.center().z() - p1.z());
					Point v2(p2.x() - p1.x(), 0.0f, p2.z() - p2.z());

					long double v4_length=((long double)v1.x() * v2.z() - (long double)v1.z() * v2.x()) *
										  ((long double)v1.x() * v2.z() - (long double)v1.z() * v2.x()) /
										  ((long double)v2.x() * v2.x() + (long double)v2.z() * v2.z());

					float dist=(float)(b.radius()*b.radius()-v4_length);
					
					if (dist>0)
						return dist;

					return 0;
				}
	};
}

#endif
